<template>
  <el-dialog
    width="1250px"
    :visible="visible"
    :lock-scroll="false"
    :destroy-on-close="true"
    custom-class="ele-dialog-form"
    :title="isUpdate?'修改座位':'添加座位'"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="132px">

      <el-form-item label="座位预定玩法:" prop="happy_id">
        <el-select filterable clearable v-model="form.happy_id" :options="happyList" size="small"
                   placeholder="-座位预定玩法-" class="ele-block">
          <el-option v-for="item in happyList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>


      <el-form-item label="座位预定场次:" prop="method_id">
        <el-select filterable clearable v-model="form.method_id" :options="happyList" size="small"
                   placeholder="-座位预定场次-" class="ele-block">
          <el-option v-for="item in methodList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>




      <el-row :gutter="15">

        <el-form-item label="状态:" prop="status">
          <el-radio-group
            v-model="form.status">
            <el-radio :label="1">开启</el-radio>
            <el-radio :label="2">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input
            clearable
            :maxlength="20"
            v-model="form.sort"
            placeholder="排序"/>
        </el-form-item>

      </el-row>
    </el-form>

    <div slot="footer">
      <el-button
        @click="updateVisible(false)">取消
      </el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="save">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

import uploadImage from '@/components/uploadImage'
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
import TinymceEditor from '@/components/TinymceEditor';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

export default {
  name: 'MemberEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
    // 栏目数据
    cateList: Array
  },
  components: {uploadImage, TinymceEditor, Treeselect},
  data() {
    return {


      // 表单数据
      form: Object.assign({status: 1}, this.data),
      // 表单验证规则
      rules: {},
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 会员等级列表

      happyList: [],
      addhourList: [],

      endhourList: [],
      methodList: [],
      houseList: [],

      is_time: 1,

    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({

        }, this.data, {});
        if(this.data.house_id){
          this.form.house_id=this.data.house_id.map(d => d.id);
        }
        this.isUpdate = true;
      } else {
        this.form = {};
        this.form.is_time = 1;
        this.isUpdate = false;
      }
    }
  },


  mounted() {
    // 获取职级列表

    this.getPackageList();
    this.getMethodList();
    this.getHouseList();


  },
  methods: {

    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          // 城市数据处理

          this.$http.post('/seat/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message({type: 'success', message: res.data.msg});
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },

    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },


    getPackageList() {
      this.$http.get('/seat/getPackageList').then(res => {
        if (res.data.code === 0) {
          this.happyList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    getHouseList() {
      this.$http.get('/seat/getHouseList').then(res => {
        if (res.data.code === 0) {
          this.houseList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    getMethodList() {
      this.$http.get('/seat/getMethodList').then(res => {
        if (res.data.code === 0) {
          this.methodList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style scoped>
</style>
